$(function () {
    //变量抽取
    var department_dataGrid = $("#department_dataGrid");
    var department_dialog = $("#department_dialog");
    var department_form = $("#department_form");


    department_dataGrid.datagrid({
        url: "/department/query.do",
        fitColumns: true,//列自适应
        fit: true,
        align: 'center',
        columns: [[
            {field: 'name', title: '部门名称', width: 100,align:'center'},
            {field: 'sn', title: '部门编号', width: 100,align:'center'},
        ]],
        toolbar: "#tb",//工具
        pagination: true,//分页
        rownumbers: true,//行号
        singleSelect: true//只允许选择一行
    })
    $("#department_dialog").dialog({
        width: 270,
        height: 140,
        buttons: "#btns",
        closed: true,
        onClose: function () {
            $("#department_form").form("clear")
        },
        resizeable:true
    })
    //方法抽成对象统一管理
    var methodObj = {//添加按钮
        add: function () {
            $("#department_dialog").dialog("open");
            $("#department_dialog").dialog("setTitle", "添加");
        },
        //编辑按钮
        edit: function () {
            var row = department_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '选择需要编辑的数据', 'info');
            } else {
                $("#department_form").form("load", row);
                $("#department_dialog").dialog("setTitle", "编辑");
                $("#department_dialog").dialog("open");
            }
        },
        del: function () {
            var row = department_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("提示", "选择要删除的数据", "info");
            } else {
                $.messager.confirm("提示", "是否要删除该条数据", function (verdict) {
                    if (verdict) {
                        $.get('/department/delete.do', {id: row.id}, function (data) {
                            if (data.success) {
                                $.messager.alert('提示', '删除成功', 'info', function () {
                                    $("#department_dialog").dialog("close");
                                    department_dataGrid.datagrid("reload");
                                })
                            } else {
                                $.messager.alert("提示", "删除失败", 'error');
                            }
                        });
                    }
                })
            }
        },
        reload: function () {
            department_dataGrid.datagrid("load");
        },
        save: function () {
            $("#department_form").form("submit", {
                url: "/department/saveOrUpdate.do",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data) {
                        $.messager.alert('提示', '保存成功', 'info', function () {
                            $("#department_dialog").dialog("close");
                            department_dataGrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert('提示', '保存失败', 'info');
                    }
                }
            })
        },
        //取消按钮
        cancel: function () {
            $("#department_dialog").dialog("close")
        }
    }

    //按钮统一绑定事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    });

});


